package com.wenhuitec.sysui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;
import com.wenhuitec.sysui.entity.FoodSkuModel;

import java.util.List;


/**
 * 商品页点击库存按钮所展示的库存管理
 */
public class ShowKucun2Adapter extends RecyclerView.Adapter<ShowKucun2Adapter.MyViewHolder> {


    private LayoutInflater inflater;
    private Context mContext;
    private List<FoodSkuModel> mFoodSkuModelList;

    public ShowKucun2Adapter(Context context, List<FoodSkuModel> foodSkuModelList) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        mFoodSkuModelList = foodSkuModelList;

    }

    public List<FoodSkuModel> getFoodSkuModelList() {
        return mFoodSkuModelList;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_kucun, parent, false);

        return new MyViewHolder(itemView);
    }


    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.stockEd.setText(String.valueOf(mFoodSkuModelList.get(position).getStock()));
        holder.skuNameTv.setText(String.valueOf(mFoodSkuModelList.get(position).getName()));
        holder.maxStockBut.setText("最大（" + mFoodSkuModelList.get(position).getMaxStock() + ")份");
        holder.maxStockBut.setOnClickListener(v -> {
            mFoodSkuModelList.get(position).setStock(mFoodSkuModelList.get(position).getMaxStock());
            holder.stockEd.setText(String.valueOf(mFoodSkuModelList.get(position).getStock()));
        });
        holder.clearBut.setOnClickListener(v -> {
            mFoodSkuModelList.get(position).setStock(0);
            holder.stockEd.setText(String.valueOf(mFoodSkuModelList.get(position).getStock()));
        });


        holder.stockEd.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            try {
                if (!hasFocus) {
                    EditText _v = (EditText) v;
                    mFoodSkuModelList.get(position).setStock(Integer.valueOf(_v.getText().toString()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mFoodSkuModelList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        EditText stockEd;
        TextView skuNameTv;
        Button maxStockBut;
        Button clearBut;

        public MyViewHolder(View itemView) {
            super(itemView);
            stockEd = itemView.findViewById(R.id.stockEd);
            skuNameTv = itemView.findViewById(R.id.skuNameTv);
            maxStockBut = itemView.findViewById(R.id.maxStockBut);
            clearBut = itemView.findViewById(R.id.clearBut);
        }
    }
}
